/*
 * testAccelerometer.c
 *
 *  Created on: 28-10-2013
 *      Author: Kuba
 */

#include "testAccelerometer.h"
/******************************************************************************
 *
 * Description:
 *  Initialize accelerometer
 *
 *****************************************************************************/
void initAccel(void){
	volatile tU32 integerResult;

	  //Initialize ADC: AIN1.7 = P0.22
	  PINSEL1 &= ~((1<<12)|(1<<13));
	  PINSEL1 |=  (1<<12);

	  AD1CR = (1 << 0)                             |  //SEL = 1, dummy channel #0
	         ((CRYSTAL_FREQUENCY *
	           PLL_FACTOR /
	           VPBDIV_FACTOR) / 4500000 - 1) << 8 |  //set clock division factor, so ADC clock is 4.5MHz
	         (0 << 16)                            |  //BURST = 0, conversions are SW controlled
	         (0 << 17)                            |  //CLKS  = 0, 11 clocks = 10-bit result
	         (1 << 21)                            |  //PDN   = 1, ADC is active
	         (1 << 24)                            |  //START = 1, start a conversion now
	         (0 << 27);							                 //EDGE  = 0, not relevant when start=1

	  //short delay and dummy read
	  osSleep(10);
	  integerResult = AD1DR;
}
/******************************************************************************
 *
 * Description:
 *  Gets difference of actual yvalue and zero value.
 *
 * Returns:
 *    difference of actual yvalue and zero value
 *
 *****************************************************************************/

int testAccel(void) {
	tS16 Yvalue=0;
	int diff=0;
	initAccel();

	osSleep(10);

	Yvalue = getAnalogueInput1(ACCEL_Y);


	diff = Yvalue - ZERO_Y;


	return diff;
}

